﻿using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using PetaPoco;
using System.ComponentModel;
using System.Linq;

namespace Dapper.Tests.Performance
{
#if !NET5_0_OR_GREATER
/*
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
   at PetaPoco.Database.PocoData.GetFactory[T](String key, Boolean ForceDateTimesToUtc, IDataReader r) in /_/benchmarks/Dapper.Tests.Performance/PetaPoco/PetaPoco.cs:line 1127
   at PetaPoco.Database.Fetch[T](String sql, Object[] args) in /_/benchmarks/Dapper.Tests.Performance/PetaPoco/PetaPoco.cs:line 458
   at Dapper.Tests.Performance.PetaPocoBenchmarks.FetchFast() in /_/benchmarks/Dapper.Tests.Performance/Benchmarks.PetaPoco.cs:line 38
   at BenchmarkDotNet.Autogenerated.Runnable_42.WorkloadActionUnroll(Int64 invokeCount) in /_/benchmarks/Dapper.Tests.Performance/bin/Release/net8.0/5e0d07b1-6b4c-4578-a0eb-d46563cab999/5e0d07b1-6b4c-4578-a0eb-d46563cab999.notcs:line 49834
   at BenchmarkDotNet.Engines.Engine.RunIteration(IterationData data)
   at BenchmarkDotNet.Engines.EngineFactory.Jit(Engine engine, Int32 jitIndex, Int32 invokeCount, Int32 unrollFactor)
   at BenchmarkDotNet.Engines.EngineFactory.CreateReadyToRun(EngineParameters engineParameters)
   at BenchmarkDotNet.Autogenerated.Runnable_42.Run(IHost host, String benchmarkName) in /_/benchmarks/Dapper.Tests.Performance/bin/Release/net8.0/5e0d07b1-6b4c-4578-a0eb-d46563cab999/5e0d07b1-6b4c-4578-a0eb-d46563cab999.notcs:line 49238
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at BenchmarkDotNet.Autogenerated.UniqueProgramName.AfterAssemblyLoadingAttached(String[] args) in /_/benchmarks/Dapper.Tests.Performance/bin/Release/net8.0/5e0d07b1-6b4c-4578-a0eb-d46563cab999/5e0d07b1-6b4c-4578-a0eb-d46563cab999.notcs:line 57
*/
    [Description("PetaPoco")]
    public class PetaPocoBenchmarks : BenchmarkBase
    {
        private Database _db, _dbFast;

        [GlobalSetup]
        public void Setup()
        {
            BaseSetup();
            RegisterSqlFactory();
            _db = new Database(ConnectionString, "System.Data.SqlClient");
            _db.OpenSharedConnection();
            _dbFast = new Database(ConnectionString, "System.Data.SqlClient");
            _dbFast.OpenSharedConnection();
            _dbFast.EnableAutoSelect = false;
            _dbFast.EnableNamedParams = false;
            _dbFast.ForceDateTimesToUtc = false;
        }

        [Benchmark(Description = "Fetch<T>")]
        public Post Fetch()
        {
            Step();
            return _db.Fetch<Post>("SELECT * from Posts where Id=@0", i).First();
        }

        [Benchmark(Description = "Fetch<T> (Fast)")]
        public Post FetchFast()
        {
            Step();
            return _dbFast.Fetch<Post>("SELECT * from Posts where Id=@0", i).First();
        }
    }
#endif
}
